﻿using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace CivilRegistry.ControlledModel
{
    /// <summary>
    /// File handler class.
    /// </summary>
    internal static class FileHandler
    {
        /// <summary>
        /// To retrive the profile image path.
        /// </summary>
        /// <param name="userId">The <see cref="User"/> id.</param>
        /// <returns>Profile image path.</returns>
        internal static string GetProfileImagePath(int userId)
        {
            FileInfo imageFile = new FileInfo(Path.Combine(GetUserFolder(userId), PathLocater.File.ProfileImage));

            if (imageFile.Exists)
            {
                return imageFile.FullName;
            }
            else
            {
                return Path.Combine(PathLocater.Folder.Images, PathLocater.File.DefaultUserImage);
            }
        }

        /// <summary>
        /// To set the profile image.
        /// </summary>
        /// <param name="userId">The <see cref="User"/> id.</param>
        /// <param name="profileImageStream">The profile image stream.</param>
        internal static void SetProfileImage(int userId, Stream profileImageStream)
        {
            using (var profileImage = Image.FromStream(profileImageStream))
            {
                profileImage.Save(Path.Combine(GetUserFolder(userId), PathLocater.File.ProfileImage), ImageFormat.Gif);
            }
        }

        /// <summary>
        /// To retrive the profile image.
        /// </summary>
        /// <param name="userId">The <see cref="User"/> id.</param>
        /// <returns>The profile image.</returns>
        internal static Image GetProfileImage(int userId)
        {
            return Image.FromFile(Path.Combine(GetUserFolder(userId), PathLocater.File.ProfileImage));
        }

        /// <summary>
        /// To retrive the <see cref="User"/> folder name.
        /// </summary>
        /// <param name="userId">The <see cref="User"/> id.</param>
        /// <returns>The user folder name.</returns>
        private static string GetUserFolder(int userId)
        {
            DirectoryInfo userFolder = new DirectoryInfo(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, PathLocater.Folder.Accounts, userId.ToString()));

            if (!userFolder.Exists)
            {
                Directory.CreateDirectory(userFolder.FullName);
            }

            return userFolder.FullName;
        }

        /// <summary>
        /// To retrive the <see cref="RationCard"/> xerox file path.
        /// </summary>
        /// <param name="userId">The <see cref="User"/> id.</param>
        /// <returns>The <see cref="RationCard"/> xerox file path.</returns>
        internal static string GetRationCardXeroxFilePath(int userId)
        {
            return Path.Combine(GetUserFolder(userId), PathLocater.File.RationCardXerox);
        }

        /// <summary>
        /// To retrive the <see cref="PermanentAddress"/> electrical bill file path.
        /// </summary>
        /// <param name="userId">The <see cref="User"/> id.</param>
        /// <returns>The <see cref="PermanentAddress"/> electrical bill file path.</returns>
        internal static string GetPermanentAddressElectricalBillFilePath(int userId)
        {
            return Path.Combine(GetUserFolder(userId), PathLocater.File.PermanentAddressElectricalBillXerox);
        }

        /// <summary>
        /// To retrive the <see cref="PresentAddress"/> electrical bill file path.
        /// </summary>
        /// <param name="userId">The <see cref="User"/> id.</param>
        /// <returns>The <see cref="PresentAddress"/> electrical bill file path.</returns>
        internal static string GetPresentAddressElectricalBillFilePath(int userId)
        {
            return Path.Combine(GetUserFolder(userId), PathLocater.File.PresentAddressElectricalBillXerox);
        }

        /// <summary>
        /// To retrive the pan card xerox file path.
        /// </summary>
        /// <param name="userId">The <see cref="User"/> id.</param>
        /// <returns>The pan card xerox file path.</returns>
        internal static string GetPanCardXeroxFilePath(int userId)
        {
            return Path.Combine(GetUserFolder(userId), PathLocater.File.PanCardXerox);
        }
    }
}